package com.nand.module.function.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.SimpleCallback;
import com.lzy.okgo.model.HttpParams;
import com.nand.library.base.base.BaseActivity;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.Callback;
import com.nand.library.base.http.MyCallback;
import com.nand.library.base.http.OkGoUtils;
import com.nand.library.zxing.activity.CaptureActivity;
import com.nand.library.zxing.activity.CodeUtils;
import com.nand.module.function.R;
import com.nand.module.function.adapter.EquipmentAdapter;
import com.nand.module.function.bean.EqDatailBean;
import com.nand.module.function.bean.MmhitchListBean;
import com.nand.module.function.constant.URL;
import com.nand.module.function.utils.LogUtils;
import com.nand.module.function.utils.SpacesItemDecoration;
import com.nand.module.function.utils.ToolUtils;
import com.nand.module.function.view.CustomPartShadowPopupView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**设备模块列表*/
@Route(path = "/function/device")
public class EqListActivity extends BaseActivity implements View.OnClickListener {
    private static final int EQUIPMENT_LEDGER_CODE = 0; //设备台账
    private static final int EQUIPMENT_INSPECTION_CODE = 1; //设备巡检
    private static final int EQUIPMENT_REPAIR_CODE = 2; //设备报修
    private static final int UPKEEP_PAGE_CODE = 3; //保养界面
    private static final int REQUEST_CODE = 1001;
    private TextView mTvHeadline;
    private ImageView mIvAdd;
    private LinearLayout mllQuery;
    private ImageView mIvOrCode;
    private EditText mEtEquipmentNumber;
    private TextView mTvQuery;
    private RecyclerView mRecycler;
    private int type;
    private List<EqDatailBean.DataListBean> list = new ArrayList<>();
    private EquipmentAdapter mRecyclerAdapter;
    private String Url = "";
    private HttpParams paramsPost;
    private LinearLayout llEmpty;
    private SmartRefreshLayout swipeRefres;
    private int page;//加载页数
    private CheckBox tvResult;//点检结果
    private CheckBox tvType;//检查类型
    private CheckBox cbState;//z状态
    private RelativeLayout relativeLayout;//筛选布局
    private RelativeLayout relativeLayoutState;//筛选布局（状态，设备保修页面）
    private int resultPosition = 0;//点检结果默认选择position
    private int typePosition = 0;//检查结果默认选择position
    private int statePosition = 0;//状态默认选择position
    private String[] resultData;//点检结果数据源
    private String[] typeData;//检查类型数据源
    private String[] stateData;//状态数据源
    private CustomPartShadowPopupView popupViewResult;
    private CustomPartShadowPopupView popupViewType;
    private CustomPartShadowPopupView popupViewState;
    private String valueParam1;//筛选条件1
    private String valueParam2;//筛选条件2
    private String valueParam3;//筛选条件3

    private String spitValueType;//设备报修，故障类别id
    private String spitValueGrade;//设备报修，故障等级

    @Override
    protected int initLayout() {
        return R.layout.activity_eq_list;
    }

    @Override
    protected void initView() {
        type = getIntent().getExtras().getInt("type");


   //     List<String> setAuth = SpUtils.getListData("setAuth", String.class);

   /*     for (int i = 0; i < setAuth.size(); i++) {
            LogUtils.e("a========="+setAuth.get(i));

        }*/
        page = 1;
        mTvHeadline = findViewById(R.id.tv_headline);
        mIvAdd = findViewById(R.id.iv_add);
        mllQuery = findViewById(R.id.ll_query);
        mIvOrCode = findViewById(R.id.iv_or_code);
        mIvOrCode.setOnClickListener(this);
        mEtEquipmentNumber = findViewById(R.id.et_equipment_number);
        mTvQuery = findViewById(R.id.tv_query);
        mRecycler = findViewById(R.id.recycler);
        findViewById(R.id.tv_query).setOnClickListener(this);
        llEmpty = findViewById(R.id.ll_empty);
        llEmpty.setVisibility(View.GONE);
        swipeRefres = findViewById(R.id.swipe_refres);

        tvResult = findViewById(R.id.cb_result);
        tvType= findViewById(R.id.cb_type);
        relativeLayout = findViewById(R.id.relative_2);
        relativeLayoutState = findViewById(R.id.relative_state);
        cbState = findViewById(R.id.cb_state);
        //imageResult = findViewById(R.id.image_result);
        //imageType = findViewById(R.id.image_type);

        swipeRefres.setEnableRefresh(true);//是否启用下拉刷新功能
        swipeRefres.setEnableLoadMore(true);//是否启用上拉加载功能
        swipeRefres.setEnableAutoLoadMore(true);//是否启用列表惯性滑动到底部时自动加载更多
        swipeRefres.setRefreshHeader(new ClassicsHeader(this));
        swipeRefres.setRefreshFooter(new ClassicsFooter(this));

        swipeRefres.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //刷新需要执行操作
                String string = mEtEquipmentNumber.getText().toString();
                page = 1;
                paramsPost = new HttpParams();
                /*if (0 < string.length()){
                    paramsPost.put("equipID", string);
                }else {
                    paramsPost.put("current", String.valueOf(page));
                    paramsPost.put("size", "10");
                }*/
                selectListCondition(false);
                //initHttpData(false);
            }
        });
        swipeRefres.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //刷新需要执行操作
                page++;
                paramsPost = new HttpParams();
                paramsPost.put("current", String.valueOf(page));
                paramsPost.put("size", "10");
                selectListCondition(false);
               // initHttpData(false);
            }
        });
        show();

        tvResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (type){
                    case 0://设备台账
                        showPopupViewResultList(view,resultData,getResources().getString(R.string.sx_zt));
                        break;
                    case 1://设备巡检

                        showPopupViewResultList(view,resultData,getResources().getString(R.string.sx_dxjg));
                        break;
                    case 2://设备报修
                        if(resultData != null && resultData.length != 0) {
                            showPopupViewResultList(view, resultData, getResources().getString(R.string.sx_gzlb));
                        }
                        break;
                    case 3://保养界面
                        showPopupViewResultList(view,resultData,getResources().getString(R.string.sx_sbby));
                        break;
                }

            }
        });
        tvType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (type){
                    case 0://设备台账
                        break;
                    case 1://设备巡检
                        showPopupViewTypeList(view,typeData,getResources().getString(R.string.sx_jclx));
                        break;
                    case 2://设备报修
                        showPopupViewTypeList(view,typeData,getResources().getString(R.string.sx_gzdj));
                        break;
                    case 3://保养界面
                        break;
                }

            }
        });
        cbState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopupViewStateList(view,stateData,getResources().getString(R.string.sx_zt));
            }
        });
    }
    /**
     *  @author ljp
     *  @time   14:11
     *  @describe 弹出筛选条件list(点巡结果,故障类别,保养状态，设备状态)
     */
    private void showPopupViewResultList(View view, String[] data, final String value){
        if(popupViewResult==null){
            popupViewResult = (CustomPartShadowPopupView) new XPopup.Builder(activity)
                    .atView(view)
                    .autoOpenSoftInput(true)
                    .setPopupCallback(new SimpleCallback() {
                        @Override
                        public void onShow() {
                        }
                        @Override
                        public void onDismiss() {
                            tvResult.setChecked(false);
                        }
                    })
                    .asCustom(new CustomPartShadowPopupView(activity));
            popupViewResult.setStringData(data, null);
            popupViewResult .setCheckedPosition(resultPosition);
            popupViewResult .setOnSelectListener(new CustomPartShadowPopupView.OnSelectListener() {
                @Override
                public void onSelect(int position, String text) {
                    if(type == 2){
                       // String[] strings = Utils.spitInfo(text, "-");
                       // text = strings[0];
                       // spitValueType = strings[1];
                    }
                    if(type == 3){
                        resultPosition = position != 0?position+3:position;
                    }else {
                        resultPosition = position;
                    }
                    tvResult.setText(position == 0 ?value:text);
                    page = 1;
                    selectListCondition(true);
                }
            });
        }
        popupViewResult.show();
    }
    /**
     *  @author ljp
     *  @time   14:11
     *  @describe 弹出筛选条件list(状态)
     */
    private void showPopupViewStateList(View view, String[] data, final String value){
        if(popupViewState==null){
            popupViewState = (CustomPartShadowPopupView) new XPopup.Builder(activity)
                    .atView(view)
                    .autoOpenSoftInput(true)
                    .setPopupCallback(new SimpleCallback() {
                        @Override
                        public void onShow() {
                        }
                        @Override
                        public void onDismiss() {
                            cbState.setChecked(false);
                        }
                    })
                    .asCustom(new CustomPartShadowPopupView(activity));
            popupViewState.setStringData(data, null);
            popupViewState .setCheckedPosition(statePosition);
            popupViewState .setOnSelectListener(new CustomPartShadowPopupView.OnSelectListener() {
                @Override
                public void onSelect(int position, String text) {
                    statePosition = position;
                    cbState.setText(position == 0 ?value:text);
                    page = 1;
                    selectListCondition(true);
                }
            });
        }
        popupViewState.show();
    }
    /**
     *  @author ljp
     *  @time   10:06
     *  @describe 根据筛选条件查询数据
     */
    private void selectListCondition(boolean isFirst){
        paramsPost.clear();
        paramsPost.put("current", String.valueOf(page));
        paramsPost.put("size", "10");
        String s = mEtEquipmentNumber.getText().toString();
        String resultValue = tvResult.getText().toString();
        if (!TextUtils.isEmpty(s)) {
                if (resultPosition > 0) {
                    if(statePosition > 0){
                        paramsPost.put(valueParam3, statePosition - 1);
                    }
                    if (typePosition > 0) {
                        paramsPost.put(valueParam1, type == 2?resultValue:String.valueOf(resultPosition - 1));
                        paramsPost.put(valueParam2, type == 2?typePosition:typePosition - 1);

                    } else {
                        paramsPost.put(valueParam1, type == 2?resultValue:String.valueOf(resultPosition - 1));
                    }
                } else {
                    if (typePosition > 0) {
                        paramsPost.put(valueParam2, type == 2?typePosition:typePosition - 1);
                    }
                    if(statePosition > 0){
                        paramsPost.put(valueParam3, statePosition - 1);
                    }
                }
                paramsPost.put("equipID", s);
            } else {
                if (resultPosition > 0) {
                    if(statePosition > 0){
                        paramsPost.put(valueParam3, statePosition - 1);
                    }
                    if (typePosition > 0) {
                        paramsPost.put(valueParam1, type == 2?resultValue:String.valueOf(resultPosition - 1));
                        paramsPost.put(valueParam2, type == 2?typePosition:typePosition - 1);
                    } else {
                        paramsPost.put(valueParam1, type == 2?resultValue:String.valueOf(resultPosition - 1));
                    }
                } else {
                    if(statePosition > 0){
                        paramsPost.put(valueParam3, statePosition - 1);
                    }
                    if (typePosition > 0) {
                        paramsPost.put(valueParam2, type == 2?typePosition:typePosition - 1);
                    }
                }
            }
        initHttpData(isFirst);
    }
    /**
     *  @author ljp
     *  @time   14:11
     *  @describe 弹出筛选条件list(检查类型，故障等级)
     */
    private void showPopupViewTypeList(View view,String[] data,final String value){
        if(popupViewType==null){
            popupViewType = (CustomPartShadowPopupView) new XPopup.Builder(activity)
                    .atView(view)
                    .autoOpenSoftInput(true)
                    .setPopupCallback(new SimpleCallback() {
                        @Override
                        public void onShow() {
                        }
                        @Override
                        public void onDismiss() {
                            tvType.setChecked(false);
                        }
                    })
                    .asCustom(new CustomPartShadowPopupView(activity));
            popupViewType.setStringData(data, null);
            popupViewType .setCheckedPosition(typePosition );
            popupViewType .setOnSelectListener(new CustomPartShadowPopupView.OnSelectListener() {
                @Override
                public void onSelect(int position, String text) {
                    if(type == 2){
                        //String[] strings = Utils.spitInfo(text, "-");
                        //text = strings[0];
                       // spitValueGrade = strings[1];
                    }
                    typePosition = position;
                    tvType.setText(position == 0 ? value:text);
                    selectListCondition(true);
                }
            });
        }
        popupViewType.show();
    }
    private void show() {
        switch (type) {
            case EQUIPMENT_LEDGER_CODE:
                mTvHeadline.setText("设备台账");
                mIvAdd.setVisibility(View.INVISIBLE);
                Url = URL.EQ_DETAIL_LIST;
                tvResult.setText("状态");
                relativeLayout.setVisibility(View.GONE);
                resultData  = getResources().getStringArray(R.array.device_state_list);
                valueParam1 = "equipState";
                break;
            case EQUIPMENT_INSPECTION_CODE:
                mTvHeadline.setText("设备点/巡检");
                Url = URL.EQ_CHECK_LIST;
                mIvAdd.setOnClickListener(this);
                tvResult.setText("点巡结果");
                tvType.setText("点/巡检");
                resultData = getResources().getStringArray(R.array.result_list);
                typeData = getResources().getStringArray(R.array.type_list);
                valueParam1 = "result";
                valueParam2 = "type";
                break;
            case EQUIPMENT_REPAIR_CODE:
                mTvHeadline.setText("设备报修");
                Url = URL.EQREPAIR_EPAIRLIST;
                mIvAdd.setOnClickListener(this);
                relativeLayoutState.setVisibility(View.VISIBLE);
                tvResult.setText("故障类别");
                tvType.setText("故障等级");
                cbState.setText("状态");
                stateData = getResources().getStringArray(R.array.iqc_device_state_list);
                valueParam1 = "hitchType";
                valueParam2 = "hitchGrand";
                valueParam3 = "state";
                //筛选条件请求（设备报修）
                httpListFilter();
                break;
            case UPKEEP_PAGE_CODE:
                mTvHeadline.setText("设备保养");
                Url = URL.MAINTENANCE_LIST;
                mIvAdd.setVisibility(View.INVISIBLE);
                tvResult.setText("保养状态");
                resultData = getResources().getStringArray(R.array.maintenanceState_list);
                relativeLayout.setVisibility(View.GONE);
                valueParam1 = "state";
                break;
            default:
                break;
        }
    }
    /**
     *  @author ljp
     *  @time   16:07
     *  @describe //筛选条件请求（设备报修）
     */
    private void httpListFilter(){
        Log.e(TAG, "httpListFilter: ---------------------");
        OkGoUtils.getInstance().postByOkGo(this, URL.MMHITCH_LIST, null, ResponseBean.class, callback2, 2);
        OkGoUtils.getInstance().postByOkGo(this, URL.MMHITCH_GRADE_LIST, null, ResponseBean.class, callback3, 3);

    }
    private Callback callback2 = new Callback<ResponseBean>() {
        @Override
        public void onSuccess(ResponseBean response, int id) {
            //Log.i(TAG, "onSuccess: ---------------------"+response.getCode());
            if (response.getCode() == 200) {
                String data = response.getDataInfo().toString();
                JSONArray jsonArray = JSONObject.parseObject(data).getJSONArray("dataList");
                resultData = new String[jsonArray.size()+1];
                //Log.e(TAG, "onSuccess: ---------callback2---"+dataList.size());
                for (int i = 0; i < jsonArray.size()+1; i++) {
                    if(i == 0) {
                        resultData[i] = "全部";
                    }else {
                        resultData[i] = jsonArray.getString(i-1);
                        //resultData[i] = dataList.get(i-1).getHitchType()+"-"+dataList.get(i-1).getHitchID();
                    }
                }

            } else {
                showToast(response.getMsg());
            }
        }

        @Override
        public void onError(Throwable e, int id) {

        }
    };
    private Callback callback3 = new Callback<ResponseBean>() {
        @Override
        public void onSuccess(ResponseBean response, int id) {
            if (response.getCode() == 200) {
                String data = response.getDataInfo().toString();
                MmhitchListBean listBeans = JSON.parseObject(data, MmhitchListBean.class);
                List<MmhitchListBean.DataListBean> dataList = listBeans.getDataList();
                typeData = new String[dataList.size()+1];
                for (int i = 0; i < dataList.size()+1; i++) {
                    if(i == 0){
                        typeData[i] = "全部";
                    }else {
                        typeData[i] = dataList.get(i-1).getHitchGraContent();
                      //  Log.e(TAG, "onSuccess: -----------getHitchGraID----"+dataList.get(i-1).getHitchGraID() );
                        Log.e(TAG, "onSuccess: -----------getHitchGraID----"+dataList.get(i-1).getHitchGraContent()+"-"+dataList.get(i-1).getHitchGraID());
                    }

                }
            } else {
                showToast(response.getMsg());
            }
        }

        @Override
        public void onError(Throwable e, int id) {

        }
    };
    @Override
    protected void initData() {
        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerAdapter = new EquipmentAdapter(EqListActivity.this, type);
        mRecycler.addItemDecoration(new SpacesItemDecoration(20));
        mRecycler.setAdapter(mRecyclerAdapter);

        mRecyclerAdapter.setOnItemClickListener(new EquipmentAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = null;
                Bundle bundle = null;
                switch (type) {
                    case EQUIPMENT_LEDGER_CODE: //设备台账
                        intent = new Intent(EqListActivity.this, EqLedgerDetalisActivity.class);
                        bundle = new Bundle();
                        bundle.putInt("id", list.get(position).getId());
                        intent.putExtras(bundle);
                        startActivity(intent);

                        break;
                    case EQUIPMENT_INSPECTION_CODE: //设备巡检
                        intent = new Intent(EqListActivity.this, PatrolPageActivity.class);
                        bundle = new Bundle();
                        bundle.putInt("type", 1);
                        bundle.putString("id", list.get(position).getId() + "");
                        intent.putExtras(bundle);
                        startActivity(intent);
                        break;
                    case EQUIPMENT_REPAIR_CODE: //设备报修
                        EqDatailBean.DataListBean dataListBean = list.get(position);
                        String state = dataListBean.getState();
                        if ("0".equals(state)) {  //未生效
                            intent = new Intent(EqListActivity.this, NewRepairActivity.class);
                            bundle = new Bundle();
                            bundle.putInt("type", 0); //0.未生效点击 1.新增
                            bundle.putString("eqID", dataListBean.getReId()+"");
                            intent.putExtras(bundle);
                            startActivityForResult(intent, EQUIPMENT_REPAIR_CODE);
                        } else {  //已生效
                            intent = new Intent(EqListActivity.this, RepairDetalisActivity.class);
                            bundle = new Bundle();
                            bundle.putInt("reID", list.get(position).getReId());
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                        break;
                    case UPKEEP_PAGE_CODE: //设备保养
                        int maintenanceState = list.get(position).getMaintenanceState();
                        if (4 == maintenanceState) {
                            intent = new Intent(EqListActivity.this, UpkeepDetalisActivity.class);
                            bundle = new Bundle();
                            bundle.putInt("type", 1); //保养完成
                            bundle.putInt("mtId", list.get(position).getMtId());
                            intent.putExtras(bundle);
                            startActivity(intent);
                        } else {
                            intent = new Intent(EqListActivity.this, UpkeepDetalisActivity.class);
                            bundle = new Bundle();
                            bundle.putInt("type", 2); //待执行
                            bundle.putInt("mtId", list.get(position).getMtId());
                            intent.putExtras(bundle);
                            startActivityForResult(intent, EQUIPMENT_REPAIR_CODE);
                        }
                        break;
                    default:
                        break;
                }

            }
        });

        paramsPost = new HttpParams();
        paramsPost.put("current", "1");
        paramsPost.put("size", "10");
        initHttpData(true);
    }

    private void initHttpData(final boolean isFirst) {
        Log.e(TAG, "initHttpData: -----paramsPost-----"+paramsPost.toString());
        OkGoUtils.getInstance().post(this, Url, paramsPost, 1, true, new MyCallback() {
            @Override
            public void onSuccess(ResponseBean response, int id) {
                if (response.getCode() == 200) {
                    String data = response.getDataInfo().toString();
                    LogUtils.e("data========="+data);
                    swipeRefres.finishLoadMore();
                    if (!isFirst)
                        swipeRefres.finishRefresh();

                    EqDatailBean listBeans = JSON.parseObject(data, EqDatailBean.class);
                    List<EqDatailBean.DataListBean> dataList = listBeans.getDataList();
                    if (page != 1) {
                        if (dataList == null || dataList.size() == 0) {
                            swipeRefres.finishLoadMoreWithNoMoreData();

                        }
                    } else {
                        list.clear();
                        if (dataList.size() == 0) {
                            llEmpty.setVisibility(View.VISIBLE);
                        } else {
                            llEmpty.setVisibility(View.GONE);
                        }

                    }
                    for (EqDatailBean.DataListBean bean : dataList) {
                        list.add(bean);
                    }
                    mRecyclerAdapter.setData(list);

                } else {
                    showToast(response.getMsg());
                }
            }

            @Override
            public void onError(Throwable e, int id) {

            }
        });

    }


    private int code = 0;
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_query) { //查询
            page = 1;
            final String s = mEtEquipmentNumber.getText().toString();
            LogUtils.e("al=========="+s);
            if (0 < s.length()){
                LogUtils.e("22al=========="+s);
                paramsPost.put("equipID", s);
            }else {
                LogUtils.e("al=========="+page);
                paramsPost.put("current", String.valueOf(page));
                paramsPost.put("size", "10");
            }
            selectListCondition(true);
            //initHttpData(true);
        } else if (view.getId() == R.id.iv_or_code) { //扫码框
            code = 0;
            Intent intent = new Intent(EqListActivity.this, CaptureActivity.class);
            startActivityForResult(intent, REQUEST_CODE);
        }else if (view.getId() == R.id.iv_add){  //添加按钮
            code = 1;
            Intent intent = new Intent(EqListActivity.this, FMainActivity.class);
            startActivityForResult(intent, REQUEST_CODE);
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        LogUtils.e("code:========="+requestCode);
        LogUtils.e("code:========="+resultCode);
        LogUtils.e("code:========="+code);
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    final String result = bundle.getString(CodeUtils.RESULT_STRING);
                    final String oid = bundle.getString(CodeUtils.RESULT_STRING2);
                    HttpParams params = new HttpParams();
                    params.put("equipID", result);
                    OkGoUtils.getInstance().post(this, URL.EFFECT_EQUIPID, params, 1, false, new MyCallback() {
                        @Override
                        public void onSuccess(ResponseBean response, int id) {
                            if (200 == response.getCode()) {
                                switch (code) {
                                    case 0:
                                        if (type == UPKEEP_PAGE_CODE) {
                                            mIvAdd.setVisibility(View.INVISIBLE);
                                            //mllQuery.setVisibility(View.GONE);
                                            Url = URL.NOT_MAINTENANCE_LIST;
                                        }
                                        mEtEquipmentNumber.setText(result);

                                        page = 1;
                                        paramsPost.put("equipID", result);
                                        initHttpData(true);

                                        mEtEquipmentNumber.setSelection(result.length()); // 将光标移至文字末尾
                                        break;
                                    case 1:
                                        if (type == UPKEEP_PAGE_CODE) {
                                            mIvAdd.setVisibility(View.INVISIBLE);
                                            mllQuery.setVisibility(View.GONE);
                                            Url = URL.NOT_MAINTENANCE_LIST;
                                        }

                                        switch (type) {
                                            case EQUIPMENT_INSPECTION_CODE:
                                                Intent intent = new Intent(EqListActivity.this, PatrolPageActivity.class);
                                                Bundle bundle1 = new Bundle();
                                                bundle1.putInt("type", 2);
                                                bundle1.putString("eqID", oid);
                                                intent.putExtras(bundle1);
                                                startActivityForResult(intent, EQUIPMENT_INSPECTION_CODE);
                                                break;
                                            case EQUIPMENT_REPAIR_CODE:
                                                LogUtils.e("accc====" + result);
                                                Intent intent2 = new Intent(EqListActivity.this, NewRepairActivity.class);
                                                Bundle bundle2 = new Bundle();
                                                bundle2.putInt("type", 1); //0.未生效点击 1.新增
                                                bundle2.putString("eqID", result);
                                                intent2.putExtras(bundle2);
                                                startActivityForResult(intent2, EQUIPMENT_REPAIR_CODE);
                                                break;
                                        }
                                        break;
                                }

                            } else {
                                showToast("设备编码不正确");
                            }
                        }

                        @Override
                        public void onError(Throwable e, int id) {
                        }
                    });

                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(EqListActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        } else {
            page = 1;
            paramsPost = new HttpParams();
            paramsPost.put("current", page);
            paramsPost.put("size", "10");
            selectListCondition(false);
            //initHttpData(false);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            //当isShouldHideInput(v, ev)为true时，表示的是点击输入框区域，则需要显示键盘，同时显示光标，反之，需要隐藏键盘、光标
            if (ToolUtils.isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    //处理Editext的光标隐藏、显示逻辑
                    mEtEquipmentNumber.clearFocus();
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }
}
